package com.usian.article.article.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.article.article.mapper.*;
import com.usian.article.article.service.ArticleService;
import com.usian.behavior.behavior.mapper.BehaviorEntryMapperMapper;
import com.usian.model.article.ApAuthor;
import com.usian.model.article.pojos.ApArticleConfig;
import com.usian.model.article.pojos.ApArticleContent;
import com.usian.model.behavior.dto.CollectionBehaviorDto;
import com.usian.model.behavior.pojo.ApBehaviorEntry;
import com.usian.model.behavior.pojo.ApCollection;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.user.pojos.ApUser;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/article/api")
public class ArticleController implements com.usian.apis.service.article.ArticleController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private ApArticleMapper apArticleMapper;
    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;
    @Autowired
    private ApArticleContextMapper apArticleContextMapper;

    @Autowired
    private ArticleMapper articleMapperl;

    @Autowired
    private BehaviorEntryMapperMapper behaviorEntryMapperMapper;

    @Autowired
    private ApCollectionBehaciorMapper apCollectionBehaciorMapper;

    @RequestMapping("findByidApAuthor/{id}")
    @Override
    public ApAuthor findByidApAuthor(@PathVariable(value = "id") Integer id) {
        ApAuthor apAuthor = articleMapperl.selectById(id);
        return apAuthor;
    }

    @RequestMapping("/findByid/{id}")
    @Override
    public ApAuthor findByid(@PathVariable(value = "id") Integer id) {
        ApAuthor one = articleService.getOne(Wrappers.<ApAuthor>lambdaQuery().eq(ApAuthor::getUserId, id));
        return one;
    }

    @RequestMapping("save")
    @Override
    public ResponseResult ss(@RequestBody ApAuthor apAuthor) {
        articleService.save(apAuthor);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @RequestMapping("/add")
    @Override
    public com.usian.model.article.pojos.ApArticle add(@RequestBody com.usian.model.article.pojos.ApArticle apArticle) {
        apArticleMapper.insert(apArticle);
        return apArticle;
    }

    @RequestMapping("/addArticleConfig")
    @Override
    public ResponseResult addArticleConfig(@RequestBody com.usian.model.article.pojos.ApArticleConfig apArticleConfig) {
        apArticleConfigMapper.insert(apArticleConfig);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @RequestMapping("/addApArticleConfig")
    @Override
    public ResponseResult addApArticleConfig(@RequestBody com.usian.model.article.pojos.ApArticleContent apArticleConfig) {
        apArticleContextMapper.insert(apArticleConfig);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @RequestMapping("findAllAr/{id}")
    @Override
    public ApAuthor findAllAr(@PathVariable(value = "id") Integer id) {
        LambdaQueryWrapper<ApAuthor> q = new LambdaQueryWrapper<>();
        q.eq(ApAuthor::getUserId,id);
        ApAuthor apAuthor = articleMapperl.selectOne(q);
        return apAuthor;
    }
    @RequestMapping("UpdateArticle_Config")
    @Override
    public void UpdateArticle_Config(@RequestParam("enable") String enable,@RequestParam("articel") String articel) {
        LambdaQueryWrapper<ApArticleConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApArticleConfig::getArticleId,articel);
        ApArticleConfig apArticleConfig1 = apArticleConfigMapper.selectOne(queryWrapper);
        //进行article_config修改
        apArticleConfig1.setArticleId(Long.parseLong(articel));
        apArticleConfig1.setIsDown(Integer.parseInt(enable)==1?true:false);
        apArticleConfigMapper.updateById(apArticleConfig1);
        return;
    }

    @RequestMapping("addApCollctionBehacior")
    @Override
    public void addApCollctionBehacior(@RequestBody CollectionBehaviorDto collectionBehaviorDto) {
        ApCollection apCollection = new ApCollection();

        apCollection.setPublishedTime(collectionBehaviorDto.getPublishedTime());
        apCollection.setCollectionTime(new Date());
        apCollection.setType(collectionBehaviorDto.getType());
        apCollection.setArticleId(collectionBehaviorDto.getEntryId());
        apCollection.setEntryId(collectionBehaviorDto.getEntry_id());

        LambdaQueryWrapper<ApCollection> q = new LambdaQueryWrapper<>();
        q.eq(ApCollection::getArticleId,collectionBehaviorDto.getEntryId());
        q.eq(ApCollection::getEntryId,collectionBehaviorDto.getEntry_id());
        ApCollection apCollection1 = apCollectionBehaciorMapper.selectOne(q);

        if(apCollection1==null){
            apCollectionBehaciorMapper.insert(apCollection);

        }else{
            apCollection.setId(apCollection1.getId());
            apCollectionBehaciorMapper.updateById(apCollection);
        }

    }

    @GetMapping("findAllArticle")
    public List<com.usian.model.article.pojos.ApArticle> findAllArticle(){
        return apArticleMapper.selectList(null);
    }

    @GetMapping("/findByIdArticleContent/{id}")
    public com.usian.model.article.pojos.ApArticleContent findByIdArticleContent(@PathVariable("id") Long id){
        return apArticleContextMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId,id));
    }


}
